﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.ColorSpaces;
using LowLevelGraphics.Types.Interval;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// This is a color filter
    /// It works like the rgb color filter, except
    /// the L, u, v values are filtered instead of R G B
    /// </summary>
    public class CIELuvFilter : BaseForeBackgroundColorFilter
    {
        protected DoubleInterval m_LInterval = new DoubleInterval(0.0f, 1.0f);
        protected DoubleInterval m_uInterval = new DoubleInterval(0.0f, 1.0f);
        protected DoubleInterval m_vInterval = new DoubleInterval(0.0f, 1.0f);

        /// <summary>
        /// default constructor
        /// </summary>
        public CIELuvFilter()
        {
        }

        /// <summary>
        /// specify intervals directly
        /// </summary>
        /// <param name="_l"></param>
        /// <param name="_u"></param>
        /// <param name="_v"></param>
        public CIELuvFilter(DoubleInterval _l, DoubleInterval _u, DoubleInterval _v)
        {
            m_LInterval = _l;
            m_uInterval = _u;
            m_vInterval = _v;
        }

        /// <summary>
        /// used by clone to create a deep copy
        /// </summary>
        /// <param name="_cieLuvFilter"></param>
        internal CIELuvFilter(CIELuvFilter _cieLuvFilter)
        {
            this.m_LInterval = _cieLuvFilter.m_LInterval;
            this.m_uInterval = _cieLuvFilter.m_uInterval;
            this.m_vInterval = _cieLuvFilter.m_vInterval;
        }

        /// <summary>
        /// gets or sets l
        /// </summary>
        public DoubleInterval L
        {
            get { return m_LInterval; }
            set { m_LInterval = value; }
        }

        /// <summary>
        /// gets or sets u
        /// </summary>
        public DoubleInterval u
        {
            get { return m_uInterval; }
            set { m_uInterval = value; }
        }

        /// <summary>
        /// gets or sets v
        /// </summary>
        public DoubleInterval v
        {
            get { return m_vInterval; }
            set { m_vInterval = value; }
        }

        /// <summary>
        /// Executes this filter
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;

            CIELuv luv = null;

            int nHeight = bitmap.Height;
            int nWidth = bitmap.Width;

            int y = 0;
            int x = 0;

            for (y = 0; y < nHeight; y++)
            {
                for (x = 0; x < nWidth; x++)
                {
                    luv = new CIELuv(bitmap.GetPixel(x, y));

                    if (m_LInterval.Contains(luv.L)
                        && m_uInterval.Contains(luv.u)
                        && m_vInterval.Contains(luv.v))
                    {
                        bitmap.SetPixel(x, y, luv.ToColor());

                    }
                    else
                    {
                        bitmap.SetPixel(x, y, m_ColorBackground);
                    }
                }
            }
            return _bitmap;
        }

        /// <summary>
        /// used to create a deep copy
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new CIELuvFilter(this);
        }
    }
}
